package com.training.bt.controller;

import com.training.bt.entity.PromotionResource;
import com.training.bt.service.IPromotionResourceService;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;

import java.util.List;

@RestController
@RequestMapping("/api/promotion-resources")
@RequiredArgsConstructor
public class PromotionResourceController {
    private final IPromotionResourceService promotionResourceService;

    // 创建关联（自动验证Token）
    @PostMapping
    public ResponseEntity<PromotionResource> createPromotionResource(
            @RequestBody PromotionResource promotionResource) {
        try {
            // 业务验证
            if (promotionResource.getPromotionId() == null || promotionResource.getResourceId() == null) {
                return ResponseEntity.badRequest().build();
            }

            // 创建关联
            PromotionResource savedEntity = promotionResourceService.createPromotionResource(promotionResource);
            return ResponseEntity.status(HttpStatus.CREATED).body(savedEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
        }
    }

    // 获取单个关联
    @GetMapping("/{relationId}")
    public ResponseEntity<PromotionResource> getPromotionResourceById(@PathVariable Long relationId) {
        return promotionResourceService.getPromotionResourceById(relationId)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    // 获取促销活动关联的资源
    @GetMapping("/promotion/{promotionId}")
    public ResponseEntity<List<PromotionResource>> getByPromotionId(@PathVariable Long promotionId) {
        List<PromotionResource> resources = promotionResourceService.getByPromotionId(promotionId);
        return ResponseEntity.ok(resources);
    }

    // 获取资源关联的促销活动
    @GetMapping("/resource/{resourceId}")
    public ResponseEntity<List<PromotionResource>> getByResourceId(@PathVariable Long resourceId) {
        List<PromotionResource> promotions = promotionResourceService.getByResourceId(resourceId);
        return ResponseEntity.ok(promotions);
    }

    // 删除单个关联
    @DeleteMapping("/{relationId}")
    public ResponseEntity<Void> deletePromotionResource(@PathVariable Long relationId) {
        promotionResourceService.deletePromotionResource(relationId);
        return ResponseEntity.noContent().build();
    }

    // 删除促销活动的所有关联
    @DeleteMapping("/promotion/{promotionId}")
    public ResponseEntity<Void> deleteByPromotionId(@PathVariable Long promotionId) {
        promotionResourceService.deleteByPromotionId(promotionId);
        return ResponseEntity.noContent().build();
    }

    // 删除资源的所有关联
    @DeleteMapping("/resource/{resourceId}")
    public ResponseEntity<Void> deleteByResourceId(@PathVariable Long resourceId) {
        promotionResourceService.deleteByResourceId(resourceId);
        return ResponseEntity.noContent().build();
    }
}